﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Threading.Tasks;
using System.Windows;
using WpfApp.Models;
using WpfApp.Views;

using System.IO;
using Newtonsoft.Json;

namespace WpfApp.ViewModels
{
    public class MyWordPageViewModel : NotificationObject
    {
        private string user;

        private ObservableCollection<Dictionary> wordBooks;

        public ObservableCollection<Dictionary> WordBooks
        {
            get { return wordBooks; }
            set
            {
                wordBooks = value;
                this.RaisePropertyChanged("WordBooks");
            }
        }

        public DelegateCommand AddCommand { get; set; }
        public DelegateCommand ShowCommand { get; set; }
        public DelegateCommand DeleteCommand { get; set; }
        public DelegateCommand RenameCommand { get; set; }

        private async void add(object obj)
        {
            await LoadWords(obj);
        }

        public async Task LoadWords(object obj)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            if (openFileDialog.ShowDialog() == true)
            {
                string filePath = openFileDialog.FileName;
                List<Word> WordList = new List<Word>();
                try
                {
                    var lines = await File.ReadAllLinesAsync(filePath);
                    foreach (var line in lines)
                    {
                        var parts = line.Split('\t');
                        if (parts.Length == 2)
                        {
                            var word = new Word
                            {
                                Name = parts[0],
                                Meaning = JsonConvert.DeserializeObject<Dictionary<string, string>>(parts[1])
                            };
                            WordList.Add(word);
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"Error reading file: {ex.Message}");
                }           
                WordBooks.Add(new Dictionary() { Title=Path.GetFileNameWithoutExtension(filePath), WordCount= WordList.Count });
                MainWindow.data.AddWordBook(MainMenu.userName, Path.GetFileNameWithoutExtension(filePath), WordList);
            }
        }

        private void show(object obj)
        {
            WordBook wordBook = new WordBook((Dictionary)obj);
            wordBook.Show();
        }

        private void delete(object obj)
        {
            MessageBox.Show("hh");
        }

        public MyWordPageViewModel(string User)
        {
            user = User;

            WordBooks = new ObservableCollection<Dictionary>(MainWindow.data.WordBook(MainMenu.userName));
            AddCommand = new DelegateCommand();
            AddCommand.ExecuteAction += new Action<object>(this.add);
            ShowCommand = new DelegateCommand();
            ShowCommand.ExecuteAction += new Action<object>(this.show);
            DeleteCommand = new DelegateCommand();
            DeleteCommand.ExecuteAction += new Action<object>(this.delete);
            RenameCommand = new DelegateCommand();
        }
    }
}
